#!/usr/bin/perl
#servermin-lib.pl

do "../web-lib.pl";
&init_config();
%access = get_module_acl();

sub krbconfig()
{
    my $domain = @_[0];
    my $pdcname = @_[1];
    my $pdcip = @_[2];
    my @krbconf;
    $krbconf[0] = "[logging]";
    $krbconf[1] = " default = FILE:/var/log/krb5libs.log";
    $krbconf[2] = " kdc = FILE:/var/log/krb5kdc.log";
    $krbconf[3] = " admin_server = FILE:/var/log/kadmind.log";
    $krbconf[4] = " ";
    $krbconf[5] = "[libdefaults]";
    $krbconf[6] = " ticket_lifetime = 24000";
    $krbconf[7] = " default_realm = ".$pdcname;
    $krbconf[8] = " dns_lookup_realm = false";
    $krbconf[9] = " dns_lookup_kdc = false";
    $krbconf[10] = " ";
    $krbconf[11] = "[realms]";
    $krbconf[12] = $pdcname." = {";
    $krbconf[13] = "kdc = ".$pdcip;
    $krbconf[14] = "default_domain = ".$pdcname;
    $krbconf[15] = "}";
    $krbconf[16] = " ";
    $krbconf[17] = "[kdc]";
    $krbconf[18] = "profile = /var/kerberos/krb5kdc/kdc.conf";
    $krbconf[19] = " ";
    $krbconf[20] = "[appdefaults]";
    $krbconf[21] = " pam = {";
    $krbconf[22] = "  debug = false";
    $krbconf[23] = "  ticket_lifetime = 36000";
    $krbconf[24] = "  renew_lifetime = 36000";
    $krbconf[25] = "  forwardable = true";
    $krbconf[26] = "  krb4_convert = false";   
    $krbconf[27] = "}"; 
    open(KR, ">/etc/krb5.conf");
    for($i=0; $i<28; $i++){
        print KR $krbconf[$i]."\n";
    }
    close(KR);
}

#mdm function for mdm module remote call
#mdm_domain($in_workgroup, $in_pdcaddr, $in_pdcuser, $in_pdcpasswd)
sub mdm_domain()
{
    my ($in_workgroup, $in_pdcaddr, $in_pdcuser, $in_pdcpasswd);
    my ($success, $workgroup, $pdcaddr, $error_msg);
    my (%econfig, $global, $out);

    $in_workgroup = @_[0];
    $in_pdcaddr = @_[1];
    $in_pdcuser = @_[2];
    $in_pdcpasswd = @_[3];

    &foreign_require("samba", "samba-lib.pl");
    $workgroup = &foreign_call("samba", "getval", "workgroup");
    $pdcaddr = &foreign_call("samba", "getval", "password server");

    $workgroup = $in_workgroup if (defined ($in_workgroup));
    $pdcaddr = $in_pdcaddr if (defined ($in_pdcaddr));

    if (($workgroup =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)\.]/)||($workgroup eq ""))
    {
	$error_msg = $text{'domain_invalid'};
	return $error_msg;
    }

    if (($in{'pdcin'} =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)\.]/))
    {
	$error_msg = $text{'pdc_invalid'};
	return $error_msg;
    }

    %econfig = &foreign_config("samba");
    &lock_file($econfig{'smb_conf'});
    $global = &foreign_call("samba", "get_share", "global");

    &foreign_call("samba","setval","password server",$pdcaddr, "");
    $success = ez_system("/usr/bin/smbpasswd", "-j", $workgroup, "-r", $pdcaddr, "-U", $in_pdcuser."\%".$in_pdcpasswd);
    if ($success == 0)
    {
	&foreign_call("samba","setval","security","domain", "");
	&foreign_call("samba","setval","workgroup",$workgroup, "");	
    }

    if ($global)
    {
	&foreign_call("samba","modify_share", "global", "global");
    }
    else
    {
	&foreign_call("samba","create_share", "global");
    }

    &unlock_file($econfig{'smb_conf'});

    $out = &ez_system("/etc/init.d/smb", "restart");

    sleep(2);

    $out = &ez_system(split(/\s/, $config{'winbind_store_passwd'}), $in_pdcuser."\%".$in_pdcpasswd);
    $out = &ez_system(split(/\s/, $config{'winbind_restart'}));
    sleep(5);

    $out = &ez_system("getent", "passwd");

    sleep(1);
		
    `$config{'winbind_info_user'} > $config{'pdcuserfile'} 2>/dev/null`;
    `$config{'winbind_info_group'} > $config{'pdcgroupfile'} 2>/dev/null`;
		
    &foreign_require("pdcusrgroup", "pdcusrgroup-lib.pl");
    $error_msg = &foreign_call("pdcusrgroup","checkpdcfile");

    return $text{'join_domain_fail'} if ($success != 0);

    return "Success";
}

#mdm function for mdm module remote call
#mdm_workgroup($in_workgroup)
sub mdm_workgroup()
{
    my ($in_workgroup, $workgroup, $out, $error_msg);
    my (%econfig, $global, $oldsecurity);

    $in_workgroup = @_[0];

    &foreign_require("samba", "samba-lib.pl");
    $workgroup = &foreign_call("samba", "getval", "workgroup");
    $oldsecurity = &foreign_call("samba", "getval", "security");

    $workgroup = $in_workgroup if (defined ($in_workgroup));

    if (($workgroup =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)\.]/)||($workgroup eq ""))
    {
	$error_msg = $text{'domain_invalid'};
	return $error_msg;
    }

    %econfig = &foreign_config("samba");
    &lock_file($econfig{'smb_conf'});
    $global = &foreign_call("samba", "get_share", "global");    

    &foreign_call("samba","setval","workgroup",$workgroup, "");
    &foreign_call("samba","setval","password server","", "");
    if ($oldsecurity eq "domain")
    {
	&foreign_call("samba","setval","security","user", "");
    }

    if ($global)
    {
	&foreign_call("samba","modify_share", "global", "global");
    }
    else
    {
	&foreign_call("samba","create_share", "global");
    }

    &unlock_file($econfig{'smb_conf'});

    $out = &ez_system("/etc/init.d/smb", "restart");

    sleep(2);

    $out = &ez_system(split(/\s/, $config{'winbind_store_passwd'}), " "."\%"." ");
    $out = &ez_system(split(/\s/, $config{'winbind_restart'}));
    sleep(5);

    $out = &ez_system("getent", "passwd");

    sleep(1);
		
    `$config{'winbind_info_user'} > $config{'pdcuserfile'} 2>/dev/null`;
    `$config{'winbind_info_group'} > $config{'pdcgroupfile'} 2>/dev/null`;
		
    &foreign_require("pdcusrgroup", "pdcusrgroup-lib.pl");
    $error_msg = &foreign_call("pdcusrgroup","checkpdcfile");

    return "Success";
}

1;
